package com.yunlong.controller;

//import com.baidubce.services.bos.demo.PutObjectDemo;
import com.yunlong.common.UserInfoDto;
import com.yunlong.common.controller.BaseController;
import com.yunlong.common.exception.SysException;
import com.yunlong.dto.LoginSuccessDto;
import com.yunlong.dto.MessageSuccessDto;
import com.yunlong.form.UpdateUserForm;
import com.yunlong.form.UpdateUserPasswordInner;
import com.yunlong.form.UserLoginForm;
import com.yunlong.form.UserPasswordResetForm;
import com.yunlong.service.UserService;
import com.yunlong.util.GetObjectDemo;
import com.yunlong.util.PutObjectDemo;
import com.yunlong.wrapper.ResultWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @author guorui
 * @date 2020-07-30 18:34
 */
@CrossOrigin
@Slf4j
@Api(value = "UsersController", tags = {"用户API"})
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<UserInfoDto> {
    @Autowired
    private UserService userService;



    /**
     * 普通用户登录
     */
    @ApiOperation("普通用户登录")
    @PostMapping("/login")
    public ResultWrapper userLogin(@Valid @RequestBody UserLoginForm userLoginForm) {
        try {
            LoginSuccessDto loginSuccessDto = userService.userLogin(userLoginForm);
            return ResultWrapper.successWithData(loginSuccessDto);
        } catch (SysException e) {
            log.info("UserController.register");
            return ResultWrapper.failure(e.getMessage());
        }
    }
    /**
     * 用户修改个人信息
     */
    @ApiOperation("用户修改个人信息（个人主页）")
    @PostMapping("/UpdateInfo")
    public ResultWrapper updateUserInfo(@Valid @RequestBody UpdateUserForm updateUserForm){
        return ResultWrapper.successWithData(userService.updateUserInfo(updateUserForm));
    }
    /**
     * 用户点击修改密码界面，所显示的个人信息
     */
    @ApiOperation("显示用户个人信息（个人主页）")
    @GetMapping("/showUserInfo")
    public ResultWrapper showUserInfo(String userId){
        com.yunlong.dto.UserInfoDto userInfoDto = userService.showUserInfo(userId);
        return ResultWrapper.successWithData(userInfoDto);
    }
    /**
     * 普通用户退出登录
     */
    @ApiOperation("普通用户退出登录")
    @PostMapping("/loginOut")
    public ResultWrapper userLoginOut(){
        try {
            log.info("用户ID为："+getCurrentUserInfo().getUserId());
            System.out.println("用户ID为："+getCurrentUserInfo().getUserId());
            String situation = userService.LoginOut(getCurrentUserInfo().getUserId());
            return ResultWrapper.successWithData(situation);
        }catch (Exception e){
            return ResultWrapper.failure(e.getMessage());
        }
    }

    /**
    *忘记密码功能，重置用户的密码
    */
    @ApiOperation(value = "忘记密码之重置密码", produces = "application/json")
    @PostMapping("/resetPasswordOuter")
    public ResultWrapper resetPasswordOuter(@Valid @RequestBody UserPasswordResetForm userPasswordResetForm){
      return ResultWrapper.successWithData(userService.resetPassword(userPasswordResetForm));
    }
    /**
     *重置用户的密码
     */
    @ApiOperation(value = "系统内重置密码", produces = "application/json")
    @PostMapping("/resetPasswordInner")
    public ResultWrapper resetPasswordInner(@Valid @RequestBody UpdateUserPasswordInner updateUserPasswordInner){
        return ResultWrapper.successWithData(userService.resetPasswordInner(updateUserPasswordInner));
    }
    /**
     * 上传用户头像
     */
    @ApiOperation(value = "上传用户个人信息（头像或者简历）")
    @PostMapping("/uploadInfo")
    public ResultWrapper uploadResume(@RequestParam("file") MultipartFile file) throws IOException {
        return PutObjectDemo.putObjectSimple(file,getCurrentUserInfo().getUserId());
    }
}
